/**
 * Created by LeungZ on 2016/7/14.
 */
import k8sTemp from './k8s/alarm.html';
import k8sCtrl from './k8s/alarm.ctrl.js';

import './k8s-pre/alarm.scss';
import k8sPreTemp from './k8s-pre/alarm.html';
import k8sPreCtrl from './k8s-pre/alarm.ctrl.js';

import detailTemp from './k8s-pre/detail/detail.html';
import detailCtrl from './k8s-pre/detail/detail.ctrl.js';

import './notification/notification.scss';
import notifTemp from './notification/notification.html';
import notifCtrl from './notification/notification.ctrl.js';

export default angular.module('ctg-k8sconsole').config(['$stateProvider', $stateProvider=> {
    $stateProvider.state('alarm-k8s', {
        url: '/alarm/k8s',
        templateUrl: k8sTemp,
        controller: k8sCtrl,
        controllerAs: 'list',
        resolve: {
            clusters: ['clusterSrv', 'ctgNotification', function (clusterSrv, ctgNotification) {
                return clusterSrv.getClusterList({perPage: 99, currentPage: 1}).catch(function (data) {
                    ctgNotification.error('获取容器集群数据有误.');
                })
            }],
            list: ['clusters', 'alarmSrv', 'ctgNotification', function (clusters, alarmSrv, ctgNotification) {
                if (clusters) {
                    return alarmSrv.getAlarmListOfK8s({perPage: 10, currentPage: 1}, {
                        isactive: true,
                        clusterInst: clusters.list.length && clusters.list[0]
                    }).catch(function (data) {
                        ctgNotification.error('获取容器集群告警列表数据有误.');
                    });
                }
            }]
        }
    }).state('alarm-k8s-pre', {
        url: '/alarm/k8s-pre',
        templateUrl: k8sPreTemp,
        controller: k8sPreCtrl,
        controllerAs: 'list',
        resolve: {
            clusters: ['clusterSrv', 'ctgNotification', function (clusterSrv, ctgNotification) {
                return clusterSrv.getClusterList({perPage: 99, currentPage: 1}).catch(function (data) {
                    ctgNotification.error('获取容器集群数据有误.');
                })
            }],
            list: ['clusters', 'alarmSrv', 'ctgNotification', function (clusters, alarmSrv, ctgNotification) {
                if (clusters) {
                    return alarmSrv.getAlertRules({currentPage: 1}, {clusterCode: clusters.list[0].clusterCode}).catch(function (data) {
                        ctgNotification.error('获取预警列表数据有误.');
                    })
                }
            }]
        }
    }).state('alarm-k8s-pre-detail', {
        url: "/alarm/k8s-pre/detail/:id",
        templateUrl: detailTemp,
        controller: detailCtrl,
        controllerAs: 'dtl',
        resolve: {
            clusters: ['clusterSrv', 'ctgNotification', function (clusterSrv, ctgNotification) {
                return clusterSrv.getClusterList({perPage: 99, currentPage: 1}).catch(function (data) {
                    ctgNotification.error('获取容器集群数据有误.');
                })
            }],
            detail: ['clusters','alarmSrv', 'ctgNotification', '$stateParams', function (clusters,alarmSrv, ctgNotification, $stateParams) {
                if ($stateParams.id && clusters) {
                    return alarmSrv.getAlertRuleDetail({id:$stateParams.id,clusterCode:clusters.list[0].clusterCode}).catch(function (data) {
                        ctgNotification.error('获取服务依赖数据有误.');
                    });
                }
            }]
        }
    }).state('alarm-notification', {
        url: "/alarm/notification",
        templateUrl: notifTemp,
        controller: notifCtrl,
        controllerAs: 'list',
        resolve: {
            list: ['alarmSrv', 'ctgNotification', function (alarmSrv, ctgNotification) {
                return alarmSrv.getNotificationList({pageNum: 1}).catch(function (data) {
                    ctgNotification.error('获取系统通知列表数据有误.');
                });
            }]
        }
    })
}]);

